package com.hu.example.controller;

import java.util.Map;

import com.hu.core.mvc.bean.ModelAndView;
import com.hu.core.util.StringUtil;
import com.hu.example.Constants;
import com.hu.example.domain.model.User;
import com.hu.example.exception.ParameterException;
import com.hu.example.exception.ServiceException;
import com.hu.example.service.UserService;

public class UserController {
    private static final String WELCOME_PAGE = Constants.JSP_PREFIX + "welcome.jsp";
    private static final String LOGIN_PAGE = Constants.JSP_PREFIX + "login.jsp";
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public ModelAndView login(Map<String, String> request, Map<String, Object> session) {
        ModelAndView modelAndView = new ModelAndView();
        if (null != session.get(Constants.USER)) {
            modelAndView.setRedirect(true);
            modelAndView.setView("dashboard.action");

            return modelAndView;
        } else {
            String lastViewPage = request.get(Constants.LAST_VIEW_PAGE);
            if (StringUtil.isEmpty(lastViewPage)) {
                lastViewPage = "";
            }

            modelAndView.addSessionAttribute(Constants.TIP_MESSAGE, null);
            modelAndView.addSessionAttribute(Constants.ERROR_MESSAGE, null);
            modelAndView.addObject(Constants.LAST_VIEW_PAGE, lastViewPage);
            modelAndView.setRedirect(false);
            modelAndView.setView(LOGIN_PAGE);

            return modelAndView;
        }
    }

    public ModelAndView saveLogin(Map<String, String> request, Map<String, Object> session) {
        String userName = request.get("userName");
        String password = request.get("password");

        ModelAndView modelAndView = new ModelAndView();
        User user = null;
        try {
            user = userService.login(userName, password);
        } catch (ParameterException e) {
            modelAndView.addSessionAttribute(Constants.ERROR_MESSAGE, e.getErrorFields());
            modelAndView.setView(LOGIN_PAGE);

            return modelAndView;
        } catch (ServiceException e) {
            modelAndView.addSessionAttribute(Constants.TIP_MESSAGE, e.getMessage() + "[" + e.getCode() + "]");
            modelAndView.setView(LOGIN_PAGE);

            return modelAndView;
        }

        modelAndView.addSessionAttribute(Constants.USER, user);
        String lastViewPage = request.get(Constants.LAST_VIEW_PAGE);
        if (StringUtil.isEmptyJSP(lastViewPage)) {
            modelAndView.setRedirect(true);
            modelAndView.setView("dashboard.action");

            return modelAndView;
        }

        String queryString = request.get(Constants.QUERY_STRING);
        System.out.println(lastViewPage + " - " + queryString);
        if (queryString.startsWith("#")) {
            lastViewPage = lastViewPage + "?" + queryString.substring(1);
        }

        modelAndView.setRedirect(true);
        modelAndView.setView(lastViewPage);

        return modelAndView;
    }

    public ModelAndView logout(Map<String, String> request, Map<String, Object> session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addSessionAttribute(Constants.TIP_MESSAGE, null);
        modelAndView.addSessionAttribute(Constants.ERROR_MESSAGE, null);
        modelAndView.addSessionAttribute(Constants.USER, null);
        modelAndView.setView(LOGIN_PAGE);

        return modelAndView;
    }

    public ModelAndView dashboard(Map<String, String> request, Map<String, Object> session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(WELCOME_PAGE);

        return modelAndView;
    }
}
